package com.melichercik.code;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.RectF;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;

public class SimpleBrick extends Brick {
	
	public String var1 = "";
	public String var2 = "";
	public String var3 = "";
	
	public SimpleBrick(String type) {
		super(type);
	}
	
	public SimpleBrick(String type, Activity activity) {
		super(type);
		createDialog(activity);
	}
	
	public void edit(Activity activity) {
		createDialog(activity);
	}
	
	public synchronized void createDialog(Activity activity) {
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
		
		dialog.setTitle(getCaption(type));
		
		dialog.setMessage("Define variables.");
		
		LinearLayout lila= new LinearLayout(activity);
		lila.setOrientation(LinearLayout.VERTICAL);
		
		final EditText input = new EditText(activity);
		input.setHint(getInputHint(type));
		lila.addView(input);
		
		final EditText input2 = new EditText(activity);
		input2.setHint("green");
		
		
		final EditText input3 = new EditText(activity);
		input3.setHint("blue");
		
		if (type == COLOR) {
			input.setHint("red");
			lila.addView(input2);
			lila.addView(input3);
		} else if (type == VARIABLE) {
			input2.setHint("value");
			lila.addView(input2);
		} else if (type == ROLL) {
			input2.setHint("speed");
			lila.addView(input2);
		}
		
		dialog.setView(lila);
		
		dialog.setPositiveButton("OK",  new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
					
				if (type == VARIABLE || type == ROLL) {
					if (!input.getText().toString().isEmpty()) {
						var1 = input.getText().toString();
					} else {
						var1 = "0";
					}
					if (!input2.getText().toString().isEmpty()) {
						var2 = input2.getText().toString();
					} else {
						var2 = "0";
					}
				} else if (type == COLOR) {
					if (!input.getText().toString().isEmpty()) {
						var1 = input.getText().toString();
					} else {
						var1 = "0";
					}
					if (!input2.getText().toString().isEmpty()) {
						var2 = input2.getText().toString();
					} else {
						var2 = "0";
					}
					if (!input3.getText().toString().isEmpty()) {
						var3 = input3.getText().toString();
					} else {
						var3 = "0";
					}
				} else {
					if (!input.getText().toString().isEmpty()) {
						var1 = input.getText().toString();
					} else {
						var1 = "0";
					}
				}
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		
		dialog.show();
		
		input.setText(var1);
		input2.setText(var2);
		input3.setText(var3);
	}
	
	public synchronized void render(Canvas canvas, int offsetX, int offsetY, int scale) {
		
		Paint myPaint = new Paint();
		
		int realX = this.xx = (x * scale) + (x * scale / 2) + offsetX;
		int realY = this.yy = (y * scale) + (y * scale / 2) + offsetY;
		this.scale = scale;
		
		myPaint.setColor(Color.rgb(6, 144, 201));
		if (type == ENDLOOP || type == ENDWHILE) {
			myPaint.setColor(Color.rgb(255, 10, 10));
		} else if (type == VARIABLE) {
			myPaint.setColor(Color.rgb(103, 38, 235));
		} else if (type == START) {
			myPaint.setColor(Color.rgb(10, 190, 10));
		} else if (type == END) {
			myPaint.setColor(Color.rgb(10, 190, 10));
		}
		
		RectF rect = new RectF(realX, realY, realX + scale, realY + scale);
		canvas.drawRoundRect(rect, (float) scale / 10,(float) scale / 10, myPaint);
		
		int lineSize = scale / 20;
		myPaint.setColor(Color.rgb(100, 100, 100));
		if (type != END) {
			canvas.drawRect(realX + scale / 2 - lineSize,
				realY + scale,
				realX + scale / 2 + lineSize,
				realY + scale + scale / 2,
				myPaint
			);
		}
		
		int lineHeight = (int) (40 * ((float) scale / 200));
		
		int realCaptionX = (x * scale) + (scale / 2) + (x * scale / 2) + offsetX;
		int realCaptionY = (int) ((y * scale) + ((scale / 2) * 1.1) + (y * scale / 2) + offsetY - ((float) lineHeight * getVarCount() / 2));
		
		int captioFontSize = (int) (40 * ((float) scale / 200));
		
		myPaint.setColor(Color.rgb(255, 255, 255));
		myPaint.setTextSize(captioFontSize);
		myPaint.setTextAlign(Align.CENTER);
		canvas.drawText(getCaption(), realCaptionX, realCaptionY, myPaint);
		
		renderVariables(canvas, realX, realY, scale, lineHeight);
	}
	
	private void renderVariables(Canvas canvas, int x, int y, int scale, int height) {
		if (type == ENDLOOP 
			|| type == ENDWHILE
			|| type == START 
			|| type == SOUND 
			|| type == END
			|| type == STOP
		) {
			return;
		}
		
		Paint myPaint = new Paint();
		myPaint.setTextAlign(Align.CENTER);
		
		int captioFontSize = (int) (35 * ((float) scale / 200));
		
		int realCaptionX = x + (scale / 2);
		int realCaptionY = y + (scale / 2) + (int) (captioFontSize * 1.3);
		
		myPaint.setColor(Color.rgb(255, 255, 255));
		myPaint.setTextSize(captioFontSize);
		
		if (type == COLOR) {
			canvas.drawText(textTruncate(var1), realCaptionX, realCaptionY - (int) (captioFontSize * 1.3), myPaint);
			canvas.drawText(textTruncate(var2), realCaptionX, realCaptionY, myPaint);
			canvas.drawText(textTruncate(var3), realCaptionX, realCaptionY + (int) (captioFontSize * 1.3), myPaint);
		} else if(type == VARIABLE || type == ROLL) {
			canvas.drawText(textTruncate(var1), realCaptionX, realCaptionY - (int) (captioFontSize * 1.3 / 2), myPaint);
			canvas.drawText(textTruncate(var2), realCaptionX, realCaptionY + (int) (captioFontSize * 1.3 / 2), myPaint);
		} else {
			canvas.drawText(textTruncate(var1), realCaptionX, realCaptionY, myPaint);
		}
		
	}
	
	private int getVarCount() {
		int count = 0;
		
		if (type == PRINT || type == DELAY || type == LED) {
			count = 1;
		} else if (type == ROLL	||type == VARIABLE) {
			count = 2;
		} else if (type == COLOR) {
			count = 3;
		}
				
		return count;
	}
	
	private static String getInputHint(String type) {
		String result = "";
		
		if (type == ROLL) {
			result = "heading";
		} else if (type == PRINT) {
			result = "message";
		} else if (type == LED) {
			result = "intesity";
		} else if (type == DELAY) {
			result = "ms";
		} else if (type == VARIABLE) {
			result = "name";
		}
		
		return result;
	}
}
